package com.wwj.auction.order.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wwj.auction.common.entity.to.CreateOrderTo;
import com.wwj.auction.common.result.R;
import com.wwj.auction.order.client.HfbClient;
import com.wwj.auction.order.client.ServiceLoginClient;
import com.wwj.auction.order.entity.ShopOrder;
import com.wwj.auction.order.entity.vo.OrderVo;
import com.wwj.auction.order.entity.vo.PayInfoVo;
import com.wwj.auction.order.entity.vo.SimpleOrderVo;
import com.wwj.auction.order.mapper.OrderMapper;
import com.wwj.auction.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wwj
 * @since 2021-08-30
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private HfbClient hfbClient;
    @Autowired
    private ServiceLoginClient serviceLoginClient;
    @Resource
    private OrderMapper orderMapper;

    /**
     * 提供远程调用
     * 获取买家和商品信息，生成订单
     *
     * @param createOrderTo
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody CreateOrderTo createOrderTo) {
        int result = orderService.create(createOrderTo);
        if (result == 1) { // 用户未绑定
//            throw new BusinessException(ResponseEnum.USER_NO_BIND_ERROR);
            return R.ok();
        } else {
            return R.error();
        }
    }

    @GetMapping("/get/{shopId}")
    public R get(@PathVariable("shopId") Integer shopId) {
        OrderVo orderVo = orderService.get(shopId);
        return R.ok().data("order", orderVo);
    }

    @PostMapping("/pay")
    public R pay(@RequestBody PayInfoVo payInfoVo) {
        Integer userId = payInfoVo.getUserId();
        String password = payInfoVo.getPassword();
        String orderNo = payInfoVo.getOrderNo();
        // 远程调用汇付宝查询当前用户的支付密码
        String hfbPassword = hfbClient.getPassword(userId);
        System.out.println(password);
        System.out.println(hfbPassword);
        if (password.equals(hfbPassword)) {
            // 若密码一致，支付成功
            // 通过订单号查询买家id和商品id
            List<Integer> ids = orderService.getIdByOrderNo(orderNo);
            // TODO 调用远程服务对账户余额进行扣减
            ShopOrder shopOrder = orderMapper.selectOne(
                    Wrappers.lambdaQuery(ShopOrder.class)
                            .eq(ShopOrder::getOrderNo, orderNo));
            // 查询拍品价格
            BigDecimal shopPrice = shopOrder.getShopPrice();
            // 查询保证金
            BigDecimal earnestMoney = shopOrder.getEarnestMoney();
            // 查询运费
            BigDecimal shopFreight = shopOrder.getShopFreight();
            // 扣减余额
            serviceLoginClient.subtractAmount(userId, shopPrice, earnestMoney, shopFreight);
            // TODO 调用远程服务生成买家的购买记录
            serviceLoginClient.create(ids.get(0), ids.get(1), orderNo);

            return R.ok().message("支付成功");
        } else {
            // 否则，支付失败
            return R.ok().message("支付密码错误");
        }
    }

    /**
     * 统计系统的成交量和交易额
     *
     * @return
     */
    @GetMapping("/get/volume/amount")
    public R getVolumeAndAmount() {
        Map<String, String> map = orderService.getVolumeAndAmount();
        return R.ok().data("map", map);
    }

    /**
     * 获取本年度每月的交易额信息
     *
     * @return
     */
    @GetMapping("/get/amount")
    public R getAmount() {
        List<BigDecimal> list = orderService.getAmount();
        return R.ok().data("list", list);
    }

    /**
     * 获取订单信息
     *
     * @return
     */
    @GetMapping("/list")
    public R getOrderList() {
        List<SimpleOrderVo> list = orderService.getOrderList();
        return R.ok().data("list", list);
    }

    /**
     * 提供远程服务
     *
     * @param orderNo
     * @return
     */
    @GetMapping("/get/order/{orderNo}")
    public R getOrder(@PathVariable("orderNo") String orderNo) {
        ShopOrder shopOrder = orderService.getOrder(orderNo);
        return R.ok().data("shopOrder", shopOrder);
    }
}

